package com.zeng.sim.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zeng.sim.controller.domain.SysDictType;
import com.zeng.sim.controller.domain.request.PageDomain;
import com.zeng.sim.mapper.SysDictTypeMapper;
import com.zeng.sim.mapper.model.SysDictDataEntity;
import com.zeng.sim.mapper.model.SysDictTypeEntity;
import com.zeng.sim.service.SysDictDataService;
import com.zeng.sim.service.SysDictTypeService;
import com.zeng.sim.util.SimConstant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author zengwanlin
 * @since 2023-12-20
 */
@Service
public class SysDictTypeServiceImpl extends ServiceImpl<SysDictTypeMapper, SysDictTypeEntity>
        implements SysDictTypeService {

    @Autowired
    private SysDictDataService sysDictDataService;

    @Override
    public Page<SysDictType> pageQuery(SysDictType sysDictType, PageDomain pageDomain) {
        Page page = Page.of(pageDomain.getPage(), pageDomain.getLimit());
        LambdaQueryWrapper<SysDictTypeEntity> wrapper = new LambdaQueryWrapper<>();
        if (StringUtils.hasLength(sysDictType.getTypeCode())) {
            wrapper.eq(SysDictTypeEntity::getTypeCode, sysDictType.getTypeCode());
        }
        if (StringUtils.hasLength(sysDictType.getTypeName())) {
            wrapper.like(SysDictTypeEntity::getTypeName, sysDictType.getTypeName());
        }
        return page(page, wrapper);
    }

    @Override
    public boolean saveSysDictType(SysDictType sysDictType) {
        SysDictTypeEntity entity = new SysDictTypeEntity();
        entity.setTypeCode(sysDictType.getTypeCode());
        entity.setTypeName(sysDictType.getTypeName());
        entity.setDescription(sysDictType.getDescription());
        entity.setValidFlag(sysDictType.getValidFlag());
        entity.setRemark(sysDictType.getRemark());
        return save(entity);
    }

    @Override
    public boolean updateSysDictType(SysDictType sysDictType) {
        SysDictTypeEntity entity = new SysDictTypeEntity();
        entity.setId(sysDictType.getId());
        entity.setTypeCode(sysDictType.getTypeCode());
        entity.setTypeName(sysDictType.getTypeName());
        entity.setDescription(sysDictType.getDescription());
        entity.setValidFlag(sysDictType.getValidFlag());
        entity.setRemark(sysDictType.getRemark());
        return updateById(entity);
    }

    @Override
    public boolean valid(Integer id) {
        SysDictTypeEntity entity = new SysDictTypeEntity();
        entity.setId(id);
        entity.setValidFlag(SimConstant.VALID_FLAG_1);
        return updateById(entity);
    }

    @Override
    public boolean invalid(Integer id) {
        SysDictTypeEntity entity = new SysDictTypeEntity();
        entity.setId(id);
        entity.setValidFlag(SimConstant.VALID_FLAG_0);
        return updateById(entity);
    }

    @Override
    @Transactional
    public boolean batchRemove(String ids) {
        String[] idArr = ids.split(",");
        SysDictTypeEntity dictTypeEntity = null;
        LambdaUpdateWrapper<SysDictDataEntity> wrapper = new LambdaUpdateWrapper<>();
        for (String id : idArr) {
            if (!StringUtils.hasLength(id)) {
                continue;
            }
            dictTypeEntity = getById(Integer.parseInt(id));
            wrapper.clear();
            wrapper.eq(SysDictDataEntity::getTypeCode, dictTypeEntity.getTypeCode());
            sysDictDataService.remove(wrapper);
            removeById(dictTypeEntity.getId());
        }
        return true;
    }

}
